package controllers.listeners.lexicon;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import controllers.shared.GetInputLoop;

import models.LexiconModel;
import models.OntologyModel;
import objects.Concept;
import objects.ConceptList;
import objects.Feature;
import objects.FeatureList;
import objects.LexiconList;
import objects.Stem;
import views.ViewLexicon;

/* Edits the lexicon concept and sense, the feature or the forms */
/* actionPerformed when you click on the table */
public class LexiconTableListener implements MouseListener{

	ViewLexicon view;
	
	public void setView(ViewLexicon view) {
		this.view = view;
	}
	
	@Override
	public void mouseClicked(MouseEvent evt) {
		int clickCount = evt.getClickCount();
		JTable table = (JTable) evt.getSource();
		
		if (clickCount < 2) {
			return;
		}
		
		if (view.findColumn("Stem") == table.getSelectedColumn()) {
			editStem(table);
		}
		
		else if(view.getButtonSelected().equalsIgnoreCase("stems")) {
			editConcept(table);
		}
		
		else if(view.getButtonSelected().equalsIgnoreCase("features")) {
			editFeature(table);
		}
		
		else if(view.getButtonSelected().equalsIgnoreCase("forms")) {
			editForm(table);
		}
		
		view.refreshTable();
	}
	
	private void editStem(JTable table) {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		
		for (LexiconList currentLexiconList : LexiconModel.getLexicon().getLexiconList()) {
			if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
				Stem selectedStem = currentLexiconList.getStemList().get(row);
				
				/* Edit Stem */
				if (column == view.findColumn("Stem")) {
					if (tableModel.getValueAt(row, column).toString().equals(selectedStem.getValue())) {
						String input = null;
						
						GetInputLoop.setInputDialog("Enter a new stem value:", "Edit Stem Value", JOptionPane.OK_CANCEL_OPTION);
						GetInputLoop.setErrorDialog("Please enter a stem value.", "Error");
						input = GetInputLoop.getInput();
						
						if (input == null) {
							return;
						}
						
						selectedStem.setValue(input);
					}
				}
			}
		}
		
		view.refreshTable();
	}
	
	private void editConcept(JTable table) {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		
		for (LexiconList currentLexiconList : LexiconModel.getLexicon().getLexiconList()) {
			if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
				Stem selectedStem = currentLexiconList.getStemList().get(row);
				ArrayList<ConceptList> ontologyList = OntologyModel.getOntology();
				
				for (ConceptList currentConceptList : ontologyList) {
					if (view.getSelectedCategory().equalsIgnoreCase(currentConceptList.getLabel())) {
						String selectedTableConcept = tableModel.getValueAt(row, view.findColumn("Concept")).toString();
						
						if (selectedTableConcept.equals(selectedStem.getConcept()) &&
							tableModel.getValueAt(row, view.findColumn("Sense")).toString().equals(selectedStem.getSense())) {
							view.removeAllSenses();
							
							/* Edit concept */
							if (column == view.findColumn("Concept")) {
								view.removeAllConcepts();
								
								for (Concept currentConcept : currentConceptList.getConceptList()) {
									view.addConceptToChooser(currentConcept.getName());
								}
								
								Object[] message = {  
									"Select concept value:", view.getCmbConcept(),  
									"Select sense value:", view.getCmbSense()
								};
								
								int option = JOptionPane.showConfirmDialog(new JFrame(), message, "Edit Lexicon Concept & Sense", JOptionPane.OK_CANCEL_OPTION);
								
								if (option == JOptionPane.OK_OPTION && view.getCmbConcept() != null && view.getCmbSense() != null) {  
									selectedStem.setConcept(view.getSelectedConcept());
									selectedStem.setSense(view.getSelectedSense());
								}
							}
							/* Edit sense */
							else if (column == view.findColumn("Sense")) {
								for (Concept currentConcept : currentConceptList.getConceptList()) {
									if (selectedTableConcept.equals(currentConcept.getName())) {
										view.addSenseToChooser(currentConcept.getSense());
									}
								}
								
								Object[] message = {"Select sense value:", view.getCmbSense()};
								
								int option = JOptionPane.showConfirmDialog(new JFrame(), message, "Edit Sense", JOptionPane.OK_CANCEL_OPTION);  
								
								if (option == JOptionPane.OK_OPTION && view.getCmbSense() != null) {
									selectedStem.setSense(view.getSelectedSense());
								}
								
							}
						}
					}
				}
			}
		}
	}
	
	private void editFeature(JTable table) {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		
		for (LexiconList currentLexiconList : LexiconModel.getLexicon().getLexiconList()) {
			if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
				Stem selectedStem = currentLexiconList.getStemList().get(row);
				Feature selectedFeature = selectedStem.getFeatureList().get(column - 1);
				FeatureList selectedFeatureList = currentLexiconList.getFeatureList().get(column - 1);
				/* (column) - 1 because the Stem column adds 1 */
				
				if (tableModel.getValueAt(row, column).toString().equals(selectedFeature.getValue())) {
					JComboBox<String> cmbValue = new JComboBox<String>();
					for (String currentValue : selectedFeatureList.getValues()) {
						cmbValue.addItem(currentValue);
					}
					
					Object[] message = {"Select new feature value:", cmbValue};
					
					int option = JOptionPane.showConfirmDialog(new JFrame(), message, "Edit Feature", JOptionPane.OK_CANCEL_OPTION);  
					
					if (option == JOptionPane.OK_OPTION) {
						selectedFeature.setValue(cmbValue.getSelectedItem().toString());
					}
					break;
				}
			}
		}
	}
	
	private void editForm(JTable table) {
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		
		for (LexiconList currentLexiconList : LexiconModel.getLexicon().getLexiconList()) {
			if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
				Stem selectedStem = currentLexiconList.getStemList().get(row);
				String input = null;
				
				GetInputLoop.setInputDialog("Enter new form value:", "Edit Form Value", JOptionPane.OK_CANCEL_OPTION);
				GetInputLoop.setErrorDialog("Please enter a form value.", "Error");
				input = GetInputLoop.getInput();
				
				if (input == null) {
					return;
				}
				
				selectedStem.getFormList().get(column - 1).setValue(input);
			}
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent arg0) {}
	
	@Override
	public void mouseExited(MouseEvent arg0) {}
	
	@Override
	public void mousePressed(MouseEvent arg0) {}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {}
}
